package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * DishController
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/28 14:08
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 创建
     *
     * @param dishDTO 菜dto
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> create(@RequestBody DishDTO dishDTO) {
        dishService.create(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO 菜页面查询dto
     * @return {@link Result}<{@link PageResult}>
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除批处理
     *
     * @param ids id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping
    public Result<?> deleteBatch(@RequestParam List<Long> ids) {
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询通过id
     *
     * @param id id
     * @return {@link Result}<{@link DishVO}>
     */
    @GetMapping("/{id}")
    public Result<DishVO> queryById(@PathVariable Long id) {
        DishVO dishVO = dishService.queryById(id);
        return Result.success(dishVO);
    }

    /**
     * 更新通过id
     *
     * @param dishDTO 菜dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateById(@RequestBody DishDTO dishDTO) {
        dishService.updateById(dishDTO);
        return Result.success();
    }

    /**
     * 按类别id列表
     *
     * @param categoryId 类别id
     * @return {@link Result}<{@link List}<{@link Dish}>>
     */
    @GetMapping("/list")
    public Result<List<DishVO>> listByCategoryId(Long categoryId) {
        List<DishVO> dishVOList = dishService.listByCategoryId(categoryId);
        return Result.success(dishVOList);
    }
}
